//
//  UUResourceManager.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 16/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUResourceManager.h"
#import <CoreText/CoreText.h>

#define ICON_FONT_NAME      (@"iconfont")

@implementation UUResourceManager

- (NSString *)iconFontName {
    return ICON_FONT_NAME;
}

- (UIFont *)iconFontWithSize:(CGFloat)size {
    NSString *fontName = ICON_FONT_NAME;
    UIFont *font = [UIFont fontWithName:fontName size:size];
    if (font == nil) {
        [self registerFontWithURL: [[NSBundle mainBundle] URLForResource:fontName withExtension:@"ttf"]];
        font = [UIFont fontWithName:fontName size:size];
        NSAssert(font, @"UIFont object should not be nil, check if the font file is added to the application bundle and you're using the correct font name.");
    }
    return font;
}

- (UIImage *)iconImageWithSize:(CGFloat)size text:(NSString *)text textColor:(UIColor *)textColor {
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat realSize = size * scale;
    
    UIFont *font = [self iconFontWithSize:realSize];
    
    UIGraphicsBeginImageContext(CGSizeMake(realSize, realSize));
    UIGraphicsGetCurrentContext();
    
    [text drawAtPoint:CGPointZero withAttributes:@{
                                                   NSFontAttributeName:font,
                                                   NSForegroundColorAttributeName: textColor
                                                   }];
    
    UIImage *image = [UIImage imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:scale
                                   orientation:UIImageOrientationUp];
    UIGraphicsEndImageContext();
    
    return image;
}

#pragma mark - 字体

- (NSString *)addIconText {
    return @"\ue725";
}

- (NSString *)helpIconText {
    return @"\ue726";
}

- (NSString *)warningIconText {
    return @"\ue737";
}

- (NSString *)seeMoreIconText {
    return @"\ue73e";
}

- (NSString *)seeOrderDetailIconText {
    return @"\ue749";
}

- (NSString *)seeIntentOrderDetailIconText {
    return @"\ue748";
}

#pragma mark - 图片

#pragma mark - 全局

- (UIImage *)navigationBarBackImage {
    NSString *text = @"\ue724";
    return [self iconImageWithSize:16 text:text textColor:[UIColor whiteColor]];
}

- (UIImage *)navigationBlackBarBackImage {
    NSString *text = @"\ue724";
    return [self iconImageWithSize:16 text:text textColor:[UIColor blackColor]];
}

#define normalImage(color)          (stretchImgFromMiddle([UIImage imageFromColor:color cornerRadius:2]))
#define highlightImage(color)       (stretchImgFromMiddle([UIImage imageFromColor:[self generateHighlightColorFromColor:color] cornerRadius:2]))

- (UIImage *)buttonImage1 {
    return normalImage(COMMON_TEXT_ORANGE_COLOR);
}

- (UIImage *)buttonImage1HL {
    return highlightImage(COMMON_TEXT_ORANGE_COLOR);
}

- (UIImage *)buttonImage2 {
    return normalImage(COMMON_TEXT_RED_COLOR);
}

- (UIImage *)buttonImage2HL {
    return highlightImage(COMMON_TEXT_RED_COLOR);
}

- (UIImage *)buttonImage3 {
    return normalImage([UIColor whiteColor]);
}

- (UIImage *)buttonImage3HL {
    return highlightImage([UIColor whiteColor]);
}

- (UIImage *)buttonImage4 {
    return normalImage(COMMON_TEXT_LIGHT_COLOR);
}

- (UIImage *)buttonImage4HL {
    return highlightImage(COMMON_TEXT_LIGHT_COLOR);
}

- (UIImage *)emptyDataImage {
    NSString *text = @"\ue74c";
    return [self iconImageWithSize:96 text:text textColor:RGB_COLOR(216, 216, 216)];
}

#pragma mark - 我的

- (UIImage *)charterProductImage {
    NSString *text = @"\ue721";
    return [self iconImageWithSize:25 text:text textColor:[UIColor blackColor]];

}

- (UIImage *)lineProductImage {
    NSString *text = @"\ue722";
    return [self iconImageWithSize:25 text:text textColor:[UIColor blackColor]];
}

#pragma mark - TabBar 图标

- (UIImage *)tabBarIconHome {
    return [UIImage imageNamed:@"TabBarIconHome"];
}

- (UIImage *)tabBarIconHomeHL {
    return [UIImage imageNamed:@"TabBarIconHome_HL"];
}

- (UIImage *)tabBarIconOrder {
    return [UIImage imageNamed:@"TabBarIconOrder"];
}

- (UIImage *)tabBarIconOrderHL {
    return [UIImage imageNamed:@"TabBarIconOrder_HL"];
}

- (UIImage *)tabBarIconNotification {
    return [UIImage imageNamed:@"TabBarIconNotification"];
}

- (UIImage *)tabBarIconNotificationHL {
    return [UIImage imageNamed:@"TabBarIconNotification_HL"];
}

- (UIImage *)tabBarIconMy {
    return [UIImage imageNamed:@"TabBarIconMy"];
}

- (UIImage *)tabBarIconMyHL {
    return [UIImage imageNamed:@"TabBarIconMy_HL"];
}

#pragma mark - Private

- (void)registerFontWithURL:(NSURL *)url {
    NSAssert([[NSFileManager defaultManager] fileExistsAtPath:[url path]], @"Font file doesn't exist");
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
    CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(newFont, nil);
    CGFontRelease(newFont);
}

- (UIColor *)generateHighlightColorFromColor:(UIColor *)color {
    CGFloat red, green, blue, alpha;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    
    red = MIN(1, red - 0.05);
    green = MIN(1, green - 0.05);
    blue = MIN(1, blue - 0.05);
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end
